/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.SyDeck.sdCities;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Properties;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;

/**
 *
 * @author Deck
 */
public class CityPath extends PlayerListener {

    static ArrayList<String> Path = new ArrayList<String>();

    public void addblock(String coord) {
        Path.add(coord);
    }

    public void remblock(String coord) {
        Path.remove(coord);
    }

    public boolean contains(String coord) {
        if (Path.contains(coord)) {
            return true;
        }
        return false;
    }

    @Override
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getPlayer().isOp() && event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && event.getPlayer().getItemInHand().getType().equals(Material.GOLD_SWORD) && !contains(event.getClickedBlock().getX() + "," + event.getClickedBlock().getY() + "," + event.getClickedBlock().getZ())) {
            event.getPlayer().sendMessage(ChatColor.YELLOW + "Block Protected!");
            addblock(event.getClickedBlock().getX() + "," + event.getClickedBlock().getY() + "," + event.getClickedBlock().getZ());
        } else if (event.getPlayer().isOp() && event.getAction().equals(Action.LEFT_CLICK_BLOCK) && event.getPlayer().getItemInHand().getType().equals(Material.GOLD_SWORD) && contains(event.getClickedBlock().getX() + "," + event.getClickedBlock().getY() + "," + event.getClickedBlock().getZ())) {
            event.getPlayer().sendMessage(ChatColor.YELLOW + "Block Protection removed!");
            remblock(event.getClickedBlock().getX() + "," + event.getClickedBlock().getY() + "," + event.getClickedBlock().getZ());
        } else if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && event.getPlayer().getItemInHand().getType().equals(Material.GOLD_PICKAXE)) {
            event.getPlayer().sendMessage(ChatColor.YELLOW + "Block X: " + event.getClickedBlock().getX() + " Z= " + event.getClickedBlock().getZ());
        } else if (event.getPlayer().isOp() && event.getAction().equals(Action.LEFT_CLICK_BLOCK) && event.getPlayer().getItemInHand().getType().equals(Material.GOLD_PICKAXE)) {
            event.getPlayer().sendMessage(ChatColor.YELLOW + "Block removed!");
            event.getClickedBlock().setType(Material.AIR);
        }
    }

    public

     void SavePath() {
        Properties prop = new Properties();
        try {
            FileInputStream in = new FileInputStream(CityInfo.Path);
            prop.load(in);
            prop.put("Path", Path.toString());
            prop.store(new FileOutputStream(CityInfo.Path), null);
        } catch (Exception ex) {
            //log.log(Level.INFO, ex.getMessage());
        }
    }

    public void LoadPath() {
        Path.clear();
        Properties prop = new Properties();
        try {
            FileInputStream in = new FileInputStream(CityInfo.Path);
            prop.load(in);
            String[] Paths = prop.getProperty("Path").replace("[", "").replace("]", "").split(",");
            prop.store(new FileOutputStream(CityInfo.Path), null);
            for (int i = 0; i < Paths.length; i = i + 3) {
                addblock(Paths[i].replace(" ", "") + "," + Paths[i + 1].replace(" ", "") + "," + Paths[i + 2].replace(" ", ""));
            }
        } catch (Exception ex) {
            //log.log(Level.INFO, ex.getMessage());
        }
    }
}
